package com.zjsru.oneDay202205;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: likew
 * @Date: 2022/5/21
 *
 * 在长度 2N 的数组中找出重复 N 次的元素
 *
 * 输入：nums = [1,2,3,3]
 * 输出：3
 *
 * 输入：nums = [5,1,5,2,5,3,5,4]
 * 输出：5
 */
public class repeatedNTimes {
    public int repeatedNTimes(int[] nums) {
        int ans = 0;
        Map<Integer,Integer> map = new HashMap<>();
        for (int i:nums) {
            map.put(i,map.getOrDefault(i,0) + 1);
        }
        for (Map.Entry<Integer, Integer> entry :map.entrySet()) {
            if(entry.getValue() > 1){
                ans = entry.getKey();
            }
        }
        return ans;
    }
    
    public static void main(String[] args) {
        repeatedNTimes repeatedNTimes = new repeatedNTimes();
        int[] nums = new int[]{5,1,5,2,5,3,5,4};
        System.out.println(repeatedNTimes.repeatedNTimes(nums));
    }
}
